package com.zxk.crawler.pipeline;

import com.alibaba.fastjson.JSON;
import com.zxk.core.common.model.News;
import com.zxk.crawler.service.EsDataHandleService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zxk
 * @description 数据pipeline
 * @date 2020/8/28
 */
@Component
public class NewsPipeline implements Pipeline {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private EsDataHandleService esDataHandleService;
    @Override
    public void process(ResultItems resultItems, Task task) {
        News news = resultItems.get("news");
        //查询es中当天该消息有没重复存入es
        Boolean isStorage = checkRepeatLib(news);
        if(isStorage) return;
        try {
            if(news != null) {
                IndexRequest request = new IndexRequest("news");
                // 规则 PUT /index/_doc/1
                request.timeout(TimeValue.timeValueSeconds(10));
                // 将数据放入请求 json
                request.source(JSON.toJSONString(news), XContentType.JSON).type("_doc");
                restHighLevelClient.index(request, RequestOptions.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Boolean checkRepeatLib(News news) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format = simpleDateFormat.format(date);
        SearchResponse search = esDataHandleService.simpleSearchNews(format);
        if(search != null) {
            SearchHit[] hits = search.getHits().getHits();
            List<String> detailLinks = Arrays.stream(hits).map(hit -> (hit.getSourceAsMap().get("detailLink") + "")).collect(Collectors.toList());
            if(null != detailLinks){
                return detailLinks.contains(news.getDetailLink());
            }
        }
        return false;
    }
}
